# -*- coding: utf-8 -*-
import time
import socket
import struct
from pathlib import Path
import os
from requests import get
import zipfile
import subprocess
import sys

############################## 配置 ##################################

port = int(sys.argv[1])
# port = 11000
folder = "E:\\Yaotai\\Yaotai_dev_2206022357\\Builds\\"
cnt = 20
account = ["wnsyrbuy", "qrzvlffm", "filuomxg", "wfmaqaks", "qmyhhukz", "mmwtyfzk", "emxnszds", "awdkaaxc", "xybpevvy", "fysutknl", "dekgnwbw", "gkptvwlx", "sdivwgfp", "temcgzli", "embkehie", "pmzepyht", "bxuwopoi", "bjqtmnik", "cqswinpw", "bufnqpvt", "viacgxdf", "mxuvhibk", "aybccsug", "bwtyopge", "nzudmyqx", "cmibxedj", "gspsbcxb", "dbbavess", "mzwcqakt", "igpzaozd", "gwxfhdhv", "ulkldlom", "gczdfejq", "kwmmtxiw", "tlnvkevh", "dvtrmyuo", "mddaqjlc", "tfyutsrw", "yugcbdqz", "zwnjtiaj", "jtxwdszo", "lqltusqa", "lpiziwwv", "hmxgpoxo", "exnabirf", "idqkuxzw", "zoyiijnt", "ongguvtl", "dkoxxoks", "jlclrnal", "yuywwpwb", "ogbvxsjm", "skbpsrgp", "fcsvyosy", "btkjohbd", "httfekgv", "ypwhjxmf", "ruuprbyp", "zaeocmsy", "uszmcwvx", "honokxsp", "xuttnnzv", "hfkhmytk", "wuiueczn", "zzotspqa", "douodcqn", "kthcaxfa", "huymtdqv", "smgihkgx", "rbiknrst", "kwsfgocg", "iaxjekbv", "hxmaiamj", "vepyunyv", "qqcazihe", "fzuzxpxx", "fvywadps", "ysqqkiuh", "xipczmnw", "fbrmdkrd", "ztepmhgq", "rogtbgxy", "iuugsnjd", "tvvpwthh", "ylpphtqr", "yduulvsp", "mdknlwkl", "brymetxs", "sigjucgh", "eseisuiq", "uywdhggh", "xamxxuvl", "eleytsjf", "cafayeyg", "oqecfsnk", "wjktpetp", "hcoevcqp", "ucvpdsnl", "exfpcuzd", "adsictqb", "cmptiekk", "ojujeaqp", "ofendfjf", "fnkmnqft", "kfhdxqdw", "ephymfsk", "bqjcczyr", "yjxxoenn", "cjleaxfe", "iclnxiko", "xknrhhvp", "ostcxtsj", "liaxskah", "sxtvvgit", "wanwxenk", "szyekupg", "vlghlrip", "bfmycfid", "rcsogbtc", "sjcvhrgl", "odtqmooy", "mvyaeuje", "zjicshmm", "nonmsosq", "nvjsbput", "mvgxvnbf", "neorycdh", "vomvurgy", "sqtvqbtl", "zjpcobrp", "ifkusrpj", "atoabpcx", "fdcbvhtr", "pzthhued", "bujmawiv", "hgwoehuo", "bgawtfog", "fiqqlflc", "pxrkwult", "bivozrio", "mgrskbcy", "zpikzhws", "upzallmt", "hkgcenxs", "wnffitzo", "zirgjdzo", "pwxgqjca", "uizdumww", "clqetmqb", "nocxtdaf", "unowslwy", "yozqxdqh", "nkbbuvcm", "ywbcluof", "cikwwbnd", "ytlqfzvn", "gksonkju", "sysalfld", "lpjzjxdd", "smwycqvq", "unlhhhrn", "jmwlqalw", "rzhgxopw", "hmxsufns", "vfwwquco", "qwxgfyud", "lgcwgnyc", "eqyczsss", "vmteflrk", "wzerscfu", "iexggnax", "vgxoegve", "ngvfsvyp", "pyobupcj", "dxcgooxs", "jgismcda", "bohzhhws", "gbxnhvvr", "wpfyibvo", "xrmuthzo", "dqufbjdy", "tiysziyd", "dssxnout", "pssjadnt", "vvutiati", "twyyvyej", "klwsflja", "tyjpgquq", "fefdoldt", "ebaqcmpk", "rovohwsg", "cvfrocjc", "krevzpzt", "xriekggs", "wlmtihtn", "dcbzswcu", "tmzqohzr", "lviwljkk", "gahbopog", "btfpjzzj", "auotvkgn", "zwyoabqd", "umxyxusf", "oosrmqfd", "slxxowqi", "nfudwbrn", "rjrxtesm", "vymxwtbr", "wvjouwqy", "hwrxbaeq", "ezbkyiam", "yhojupva", "tqxfqyfm", "rbekwmby", "mttjrvfr", "vaftmbar", "npivjqgq", "balvyaib", "iauxlzsz", "ysltubnq", "talqzrzn", "ihhjfsqy", "aybtayjw", "kcqunnrq", "xocusetb", "tallkifr", "wbtxgjer", "nwyrmsjb", "uydhvsno", "kwfdvdme", "hlzpxijy", "eyrrehpu", "tpfkqvbe", "qtgfnmex", "qrblsstd", "laussrqz", "unjyhnqn", "kbmaaazc", "qaachfab", "xbwjfwpm", "etqmgvfu", "higpvjlc", "srbdbjos", "pkmcdxtj", "lrzenjzd", "tklcjgtb", "kzfvqrlu", "wzxkfaay", "jrcybgvn", "vpkeihtd", "kbetvglx", "tyewmnxv", "cvcvqxhc", "ygpzqakq", "czqfhxwh", "tatkmsxv", "bbzydjdq", "pggqvgxt", "wrddoluz", "licihcqs", "oylnemhm", "hzqcrfoo", "pptynxps", "clptnraa", "ixnlcsnj", "xbblqabs", "ezcnopug", "plaqorta", "uhddaunm", "obeuqiaa", "zugihhut", "batuaqok", "kqnuvdap", "lylntbjp", "hgvahtiq", "unmxafsv", "thodzshr", "wfrhgxkv", "fvcoiomw", "ffozyhdo", "bevhmyyz", "kxkppjgp", "gkgoxjrl", "defgnjcz", "rrlxilhl", "nknjkgdp", "mewmhxtl", "ozjscjmp", "qpbqohby", "xzdrbsnq", "ocuhqvzh", "sshexyou", "sjlxcgrr", "dcxkzrss", "owofgeww", "baufxruc", "ufperdsb", "luhikrqn", "dihsvuoi", "mtegowcu", "iwyvyrnx", "uqrvlfzl", "ixboijxf", "cwvdwoab", "zxnopfma", "eojbybnh", "ysgfathc", "xzrpeumr", "htupwwfj", "nosvmzxh", "wmvqmfob", "imnrafzz", "nzlzzgvi", "icbfzgok", "rnsmrlve", "wckpbkud", "uykqznfv", "zajtagvo", "tfflgwxo", "ynlvebyx", "gzxydwdu", "jvpafrpg", "ugoinidc", "zaipwzlh", "niawsdfj", "ozyrksgn", "gfrbfkhp", "eugvkkcu", "bqwdpngq", "xizronvj", "fnzaxjqd", "qunfcubv", "mpbxafns", "ljxvscxu", "geifqtcx", "baqjockd", "cveldviu", "aubngjte", "rrlwnnxd", "yskxhtzv", "wkdrqbks", "ucaraxzj", "htpxsokb", "jkuymdla", "fzklqlyb", "eqsekrbj", "sabphnzs", "sfysobgl", "dbizysck", "avjdldbr", "wmihkutq", "rzvrdzbd", "vqvybjek", "ytpzkpis", "mubpcvyf", "ofklnmgm", "lvjqscbu", "dzzadlze", "btiuykjt", "oozdelpu", "wzvizeql", "vjxupgcm", "gbjvvhaj", "bewvpkck", "fyjvqrtw", "dofjsnrn", "jppavzrv", "drbsuvgx", "wsknfwqo", "kfdmfvwc", "qvsdbqia", "satxtbfb", "qikxytkw", "gbqystxi", "kbkgwjbc", "vwbbmago", "soisxvpj", "bmgddiew", "wmcnpsdz", "nisqdprz", "dsuvzwbr", "iamdkfpz", "rcxiasnm", "fkkwmisy", "dquqbzpo", "gfuycjjr", "tbsyztcu", "skronxlc", "rntoljim", "retmkqkl", "iylwvqeg", "zjwcudrq", "aukbauab", "mdvzbhyb", "gtfjrerm", "dgylqdnp", "pownhtto", "mmcmkipm", "jeapdlzt", "iltipnqt", "vowcolcj", "qgwkfimp", "rwsmfmvi", "zkacnhlq", "wzxuzeoi", "lkronaks", "osanbtix", "qyozttsy", "tmtjdbri", "zdntyhth", "lukyzirj", "wpepxmdm", "afzgallo", "oljkndhd", "ipnqyugz", "obmaxeot", "mblvuuif", "swskpyld", "lbunaxfk", "xxjvmzea", "qoddhzpw", "qdesgujk", "ifpdhfps", "tsoeuces", "spphrykv", "xogtrdtz", "ekoqrlpb", "wrosneyq", "mjsapfxf", "kzlwdzjx", "mbchrjjo", "dewiiqpt", "cigtwdra", "rrhvofkb", "sggkbgyx", "lkrgekif", "qdexcbvt", "hgksdbwv", "ksoxggvy", "rvqhloys", "rscsodvx", "biytbpom", "niabfmwx", "cpruzpgc", "uizfhmif", "gsavwnjr", "mqxjftni", "tehmikgu", "ioqybokq", "sebpwacc", "fmwuqxdy", "snkwnrjt", "wuplgnpk", "azobbqsy", "mvbcgxrr", "csjrplpy", "llfqwznj", "bcxfaxih", "gqwebzin", "otnijypl", "xmbchewi", "fvmaewso", "udhykwbh", "txsolxch", "ioixnpky", "cbbsoifp", "kvguagyj", "eqrmvemq", "piqjabnw", "nfugjpjs", "meaujgrl", "fifpbhgg", "nkfvwevb", "ovmtsyby", "oandxfbr", "jahluohj", "dhbdhzwu", "ypsftcgr", "uouvqzmq", "lcjiiean", "lobuabhq", "rfchbcxq", "cayrbzok", "oafmmexi", "cftwozqm", "uzxojmdg", "npypauon", "czxtezme", "fyfcwibn", "aqikthmb", "vpizrhiu", "hzfuebdj", "yanmkwwv", "mpldqqwi", "qttvzbno", "kwzjbrbx", "qzpvguzj", "pozhfriy", "eibecxzo", "wgucoqda", "qtonngof", "qsnnludd", "lrzxjkkp", "ezxuikvb", "edpxiwcs", "rimezxet", "iyoxjfnh", "fqxkqqty", "enjzkivn", "arhljtbk", "cltsfmbn", "nnvkyetq", "ckfbxwcy", "hdomkyvq", "eeienrpr", "mxaknayx", "lwssycwb", "spkwtuxj", "ewiixdsv", "fwtdpcqm", "wantntrl", "ohqakyxf", "bmfhdmux", "qotivvau", "obwwamnf", "wfpuelli", "sgfarphq", "tbaepujr", "yxwjpqrf", "xericrcx", "enbvhryt", "sswwicmo", "jrxmtzsi", "ffmfggun", "gudunyqr", "cndibmcy", "easmhcks", "hawykweo", "lqzlevbj", "epjtyqpk", "lfcmykwx", "dcvnhtur", "qrrtwpqx", "yojgejpz", "vzfazxwp", "ypparxkn", "zqjtmtlk", "lovviubl", "vpecowie", "gtpttrsu", "ftsjrqhz", "ivouwrns", "esomrgik", "cnourijx", "lqoayqtg", "hlhlvsvd", "clvydaso", "kaspxeui", "vocqspsk", "qctydijl", "qbjnmpnc", "dvseyzue", "xayqrist", "gnytgszz", "suytdimg", "yfafdbbh", "qwgucibj", "qpmoskcn", "mcigekou", "yfjbpykz", "ygyfemsb", "pmxaeagz", "zrwzqjyh", "wdqjweob", "xhbqljfj", "vvtielok", "obxvhkpo", "pqmgxdhr", "jbsydbiv", "iaxvnzxa", "sxkodrrh", "qsnftfnn", "vweibyrt", "eavnkljs", "rhgowvqi", "veioznaz", "pysvilee", "preflcfy", "qmtkrckn", "qbaycrhw", "jtxcefzu", "mrlriuof", "hjegnoos", "ngfyayry", "btxnifyl", "nnxeidrj", "ftmtlwub", "octurlts", "wifwgney", "hlynrluj", "xgrnxrns", "rlebcryl", "qujkcoka", "cxvevaof", "ekcgdugd", "rzdgxcpm", "jkllphdd", "ekqishln", "mfcqxsfu", "nuwngqpg", "bgsxbvxv", "pmqrjskp", "vrqfevwc", "iwgewpup", "gxqmowcu", "ykwqnijx", "tlmpsthw", "nahfxmqf", "qiatkmlo", "npoiutbz", "devoedny", "eduttvxu", "tzccewkt", "dsyewgwh", "jbezdrei", "xuozeazz", "hbrejnvy", "gcdjhtfv", "ighjyvft", "rcoenofq", "oifzrgpp", "uzwnaiee", "mbsgpqpw", "kqzqyhcn", "ovgmbnuf", "fxoblbjs", "gstbsatv", "ovhqvuuf", "xbpszpeo", "axfnqkxy", "oarjdqod", "dlzlgokm", "xmlmotgy", "myrwvahg", "lxekitch", "vndljxpp", "gkaiikzz", "afsiijsu", "actentdt", "kuaglroe", "ycindkct", "ugbrrhat", "nukjxrqd", "ymwuazoj", "jnbbonow", "lnrnvtsk", "divheipd", "vcqwwqka", "kifwurdp", "qcrkrnki", "ucuoitpt", "yvvzmsux", "tfmevptx", "hljdtctj", "oynbnsjy", "koirnbgd", "ceaauwdx", "zrxdvods", "zzwctarc", "gcjrgzmf", "oceyxdwn", "wtjpjohs", "aqhtfnuj", "xogpdkth", "iecpptiw", "ahechuez", "ydjbzxts", "dufgakya", "zydcfbfs", "usyaecof", "rfvwtrdz", "mpbmtyhg", "upddlvas", "hsbqodcy", "glyblvgm", "jzxnkply", "wyqgpfll", "oywqjejx", "sqphpswl", "jslqwjid", "wgxvsjgz", "njcrakmv", "nwvghcay", "wotevmgt", "lejisgio", "zdreovmc", "rqiztwvc", "sorrptkt", "klrzgiym", "nzxdwzfj", "cwnqdldj", "yyswhcwr", "bckptucv", "lsxpcqfv", "eugrbcpw", "zxrruths", "byxtyqgn", "hiotqagq", "lnndsqhk", "cpwhkxzs", "wwajheoz", "hnibzpki", "wnljsino", "lbdmrkef", "gzjzisvd", "qsycaweq", "gtmushyr", "zuuovkqh", "bknpulce", "hgoqrwrc", "ownjkzsc", "kbtcmpau", "hrpccsqd", "aeokooaf", "ovhygikg", "urxdkzcv", "ejhphjrv", "jabqeazw", "nzdvadau", "ftmbehan", "pmwhipdl", "igyjsdki", "agnuytcn", "egdzdwuc", "iknmfghh", "vgtiebte", "uwmtvrpm", "jbpvqqrx", "jweizehj", "wchwanwd", "bmhebrbd", "znjwcfkh", "luliizfe", "qglmwmem", "pflttrru", "nowxepuv", "xleivrxs", "yrvqomve", "ikfoxevn", "atnfeavv", "phkxgpxu", "jausbesk", "sibblecx", "nsskmrxb", "ztcfkqna", "vxupzqxp", "cjnbswnn", "fkdeokky", "kcbglazt", "bathtxap", "jhsnossv", "klrmvzsl", "rnppvicp", "kkgvoqzc", "phnrlify", "lwykfjyh", "armpbzua", "djysrvgk", "qochmvdu", "mzdfvxka", "espuvahs", "neenbspc", "uftjkkkq", "tdckfhxh", "ackkqmei", "etcjekko", "rxvucpyl", "rvohddsu", "rlrvkhbm", "dociiiou", "mckejqmr", "umqjcisk", "lzcfuavq", "qebxalni", "prdpkorl", "ikzbxqvu", "tsjshqjo", "ynsehxuk", "lrqflivx", "zasksnxw", "lfszoyyy", "zbsiopsg", "cytyhacj", "aielcrgr", "gwltgxei", "crxiwoyb", "xgherkyt", "tdjswpqb", "geuexboz", "wxwbblxe", "gobnspby", "manyrlln", "uhysnkat", "tolfjulh", "cyrtlqmo", "onlwqvbi", "kaewuqxu", "fwtpkgek", "gdvugicp", "irdvjjir", "afoyzkpa", "uyhmjdyh", "ledbogdl", "dldimbti", "ejdlnjdb", "oeswopqe", "dbwvohtu", "wsgcywpc", "priypnoc", "vxmoraiq", "wtngtejn", "dlwzegty", "gnvkrree", "igyqjask", "cikuhrfa", "cxavpmgs", "womcliin", "wgxiysbe", "deebympc", "gzydlpqv", "xfivexkp", "rsvdpplt", "lxhjikta", "owzdubkb", "gyxnwqeh", "zjtqoxwy", "ckzsofra", "dstjveav", "nplytiie", "wnsnfgkw", "mguxrdwc", "ssnmddmc", "ucfkgvbr", "etdvzlua", "aczpgthf", "lqzxmbbe", "rpvrnqdm", "nharnpll", "rtjazqpc", "yhnmuuxy", "desiepoy", "xwniamgp", "hpedwwaq", "qtqjlynw", "egwbhavb", "efhshnsa", "oaumivrb", "ldviiisb", "pzertwlz", "ljogdszt", "mtdnrpqp", "rbfuluig", "wkawfyhl", "akvqduhq", "ibhwhpil", "yvfiednt", "atmgavtn", "jtanszgr", "lbgqyyrb", "drdfvrfw", "fpkkirib", "uacfjpai", "shnihrqc", "qdkeeoms", "hkvhktav", "ybbkmlfg", "rwpfajzu", "ozijvtlf", "dafqbuwk", "bgtpvzxi", "anpyseqf", "dxovzouk", "hiovoptl", "sronwzdw", "liambkvw", "rlbulapr", "vceejneb", "bzqfpbjc", "ukwrnbpg", "ipurggqz", "rkrxiacf", "pchtwslg", "whvyryrv", "scpjgrjj", "gvamvafr", "zbdmsize", "pzlvutjy", "gsgbdvci", "xuxubolk", "bwjmjxqv", "plgccady", "iwohpaip", "orprzhhp", "nchreyrq", "zbsfcsyw", "cfozvjdw", "ayfmnxkv", "hrwsmmnz", "wqxakjer", "oflczfcd", "hzwjqqgx", "wzzsebot", "zgyupckq", "zgrpaljs", "jnmmauqv", "wzdfndtb", "rqqyzcgw", "yqkfsdox", "tzjjyiov", "eclhhoyl", "nuxnmfaa", "ntbkroii", "zdquntat", "qxghypvw", "xjtmhabz", "ljrpjalz", "rymbrcet", "ymotffoe", "limvokal", "kjumkvme", "kqaauocg", "tkxohwtj", "xhujapwh", "rcywnnyg", "zvhthpxn", "zijbbxlg", "psiutqcp", "zvffjwiy", "bzbtwqeb", "vgisbmbo", "qswqkuey", "gayyjtyy", "hvrpjhop", "bdhnjwgn", "upawyctr", "rrrdqosv", "ajoxtsbv", "ozfqmmhj", "vymqtkpc", "rfvrolbr", "mvyinmay", "pbizhcdc", "jfyprzud", "qmksrkzo", "rhelxmhr", "ovsgknfp", "wwdkhunb", "tppfdwfr", "tctncybb", "xlqcjiko", "elcdyigw", "qxshvxrf", "tydcgiar", "qakpohpy", "yigmdppf", "nnhyagus", "oehrvllj", "rrxfqmsn", "xnhlvhvi", "mbnmzeee", "ldotmymj", "haoivana", "qyypmhio", "cxnvlsok", "peycixhl", "lcghauow", "aaweskzl", "irhpyxyv", "bgnfgfpn", "bnxxteuq", "ayisawsm", "ausgcdag", "wymzknso", "zoqqyvmg", "kjdhqeds", "mkipyuag", "jqtyxqlm", "oolyxcfl", "wwkurdpn", "zuojomma", "ecrwvtze", "pkmwvvge", "gfonfgbi", "wciqtjbp", "exkyytcs", "yduhiibz", "ukaawgez", "dyvqbdee", "himkwzhd", "vziunwqj"]

server_ip = "apps-qa.danlu.netease.com"
server_port = 35254
gm = """
GMCmdLogin -cmd_account_on -loading
GMCmdWait -time 10

"""

############################### 代码 #################################


def get_account_list(cnt):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((server_ip, server_port))
    buffer = struct.pack('i', cnt)
    s.send(buffer)
    msg = s.recv(1024)
    id = struct.unpack("i", msg)[0]
    s.close()

    acc_len = len(account)
    id_max = int(acc_len / cnt)
    id = id % id_max
    ret = []
    for i in range(cnt):
        acc_idx = id * cnt + i
        if acc_idx < acc_len:
            ret.append(account[acc_idx])
    return ret

def start(acc, cmd):
    import subprocess
    def start_server(cmd, cwd):
        return subprocess.Popen(cmd, cwd=cwd, creationflags=subprocess.CREATE_NEW_CONSOLE)

    start_server(cmd + " -account "+acc, folder)

def init():
    p_str = "C:\\users\\netease\\frp"
    path = Path(p_str)
    path.mkdir(parents=True, exist_ok=True)

    # Get the current working directory
    cwd = os.getcwd()

    # Print the current working directory
    print("Current working directory: {0}".format(cwd))

    # Change the current working directory
    os.chdir(p_str)

    # Print the current working directory
    print("Current working directory: {0}".format(os.getcwd()))

    frp_file_url = "https://gitlab.com/wsszh/file-server/-/raw/main/frp_0.38.0_windows_test.zip?inline=false"
    frp_path = "frp_0.38.0_windows_test"
    frp_file = frp_path + ".zip"

    # download frp
    with open(frp_file, "wb") as file:
        resp = get(frp_file_url)
        file.write(resp.content)

    # unzip
    with zipfile.ZipFile(frp_file, "r") as zip_ref:
        zip_ref.extractall(".")

    os.chdir(frp_path)

    in_file = r'frpc-yaotai.ini'
    replace_in_file(in_file, in_file, "[RDP]", "[RPD-{port}]".format(port=port))
    replace_in_file(in_file, in_file, "RDP-PORT", str(port))

    subprocess.Popen(["frpc.exe", "-c", in_file])
    subprocess.Popen(["start", "powershell"], shell = True)

def replace_in_file(in_file, out_file, search_text, replace_text):
    # Opening our text file in read only
    # mode using the open() function
    with open(in_file, 'r') as file:

        # Reading the content of the file
        # using the read() function and storing
        # them in a new variable
        data = file.read()

        # Searching and replacing the text
        # using the replace() function
        data = data.replace(search_text, replace_text)

    # Opening our text file in write only
    # mode to write the replaced content
    with open(out_file, 'w') as file:

        # Writing the replaced data in our
        # text file
        file.write(data)

if __name__ == "__main__":
    init()
    with open(folder + "gm_cmd_sample.txt", "w+", encoding='utf-8') as f:
        f.write(gm)
    acc_list = get_account_list(cnt)
    now_cnt = 0
    for acc in acc_list:
        time.sleep(5)
        now_cnt = now_cnt + 1
        #暂定最多开15个
        if now_cnt > cnt:
            break
        start(acc, folder + "yaotai.exe -gm -gm_file " + folder + "gm_cmd_sample.txt -batchmode -nographics -disable_guide -fps 45 -url apps-qa.danlu.netease.com -port 35021")
